package trong.gold.soft.com.model;

import java.io.*;
import javax.microedition.rms.RecordStore;

public class Record
{

    private static RecordStore rs;

    public Record()
    {
    }

    public static void open()
    {
        try
        {
            rs = RecordStore.openRecordStore(Resources.QRS, true);
        }
        catch(Exception e) { }
    }

    public static void close()
    {
        try
        {
            rs.closeRecordStore();
        }
        catch(Exception e) { }
    }

    public static void delete()
    {
        if(RecordStore.listRecordStores() != null)
            try
            {
                RecordStore.deleteRecordStore(Resources.QRS);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
    }

    public static void save()
    {
        try
        {
            ByteArrayOutputStream arrBytes = new ByteArrayOutputStream();
            DataOutputStream Data = new DataOutputStream(arrBytes);
            for(int i = 0; i < 5; i++)
            {
                Data.writeUTF(Resources.highscore[i]);
                Data.flush();
                byte b[] = arrBytes.toByteArray();
                int num = rs.addRecord(b, 0, b.length);
                arrBytes.reset();
            }

            arrBytes.close();
            Data.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void load()
    {
        try
        {
            byte b[] = new byte[50];
            ByteArrayInputStream arrBytes = new ByteArrayInputStream(b);
            DataInputStream Data = new DataInputStream(arrBytes);
            for(int i = 1; i <= rs.getNumRecords(); i++)
            {
                rs.getRecord(i, b, 0);
                String s = Data.readUTF();
                Resources.highscore[i - 1] = s;
                arrBytes.reset();
            }

            arrBytes.close();
            Data.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public static String getLevel(int x)
    {
        return String.valueOf(x);
    }
}